package com.sunflower.common.mybatis.core.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.SqlCondition;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.sunflower.common.core.utils.MapstructUtils;
import com.sunflower.common.mybatis.core.domain.ISearch;
import com.sunflower.common.mybatis.core.mapper.BaseMapperPlus;
import com.sunflower.common.mybatis.core.page.PageQuery;
import com.sunflower.common.mybatis.core.page.TableDataInfo;
import com.sunflower.common.mybatis.core.service.IBoSearchService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author: SunLuQuan
 * @Date: 2024年05月08日 15:28
 * @Description: BoSearchServiceImpl
 */
public class BoSearchServiceImpl<T extends ISearch, M extends BaseMapperPlus<T, V>, V, B extends ISearch> extends BoServiceImpl<T, M, V, B> implements IBoSearchService<T, V, B> {
    @Override
    public TableDataInfo<V> searchPageList(B bo, PageQuery pageQuery, SFunction<T, ?>... columns) {
        T entity = MapstructUtils.convert(bo, getEntityClass());
        TableInfo tableInfo = TableInfoHelper.getTableInfo(getEntityClass());
        List<TableFieldInfo> fieldList = tableInfo.getFieldList();

        QueryWrapper<T> queryWrapper = Wrappers.query(entity);
        if (StrUtil.isNotEmpty(entity.getSearchValue())) {
            fieldList.stream().filter(tableFieldInfo -> {
                /**
                 * 注意这里只能使用一下几种模糊查询才会生效 不能使用自定义的，如果使用自定义的请更改以下逻辑
                 */
                if (StrUtil.equalsAny(tableFieldInfo.getCondition(), SqlCondition.LIKE, SqlCondition.LIKE_RIGHT, SqlCondition.LIKE_LEFT, SqlCondition.ORACLE_LIKE)) {
                    if (tableFieldInfo.getPropertyType() == String.class) {
                        // 只对空值做处理
                        String value = Convert.toStr(ReflectUtil.getFieldValue(entity, tableFieldInfo.getField()));
                        return StrUtil.isEmpty(value);
                    }
                }
                return Boolean.FALSE;
            }).collect(Collectors.toList()).forEach(tableFieldInfo ->
                    queryWrapper.and((wrapper) -> {
                        switch (tableFieldInfo.getCondition()) {
                            case SqlCondition.LIKE:
                            case SqlCondition.ORACLE_LIKE:
                                wrapper.like(tableFieldInfo.getColumn(), entity.getSearchValue());
                                break;
                            case SqlCondition.LIKE_RIGHT:
                                wrapper.likeRight(tableFieldInfo.getColumn(), entity.getSearchValue());
                                break;
                            case SqlCondition.LIKE_LEFT:
                                wrapper.likeLeft(tableFieldInfo.getColumn(), entity.getSearchValue());
                                break;
                        }
                    })
            );
        }
        return null;
    }

    @Override
    public List<V> searchList(B bo) {
        return null;
    }
}
